package com.wms.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.context.BaseContext;
import com.wms.dto.UserPageQueryDTO;
import com.wms.entity.WmsUser;
import com.wms.mapper.RoleMapper;
import com.wms.mapper.UserMapper;
import com.wms.mapper.UserRoleMapper;
import com.wms.result.PageResult;
import com.wms.service.UserService;
import com.wms.vo.UserRoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public WmsUser findByUserName(String name) {
        WmsUser user = userMapper.findByUserName(name);
        return user;
    }

    @Override
    public UserRoleVo login(UserRoleVo user) {
        String userName = user.getUserName();
        String password = user.getPassword();
//        WmsUser info = userMapper.findByUserName(userName);
        UserRoleVo info = userMapper.getUserRoleVoById(userName);
        if (info != null && info.getPassword().equals(password) && info.getStatus().equals("0")){
            WmsUser temp = new WmsUser();
            BeanUtils.copyProperties(info,temp);
            temp.setLoginDate(LocalDateTime.now());
            userMapper.update(temp);
            info.setPassword(null);
            return info;
        }
        return null;
    }

    /**
     * 分页查询
     * @param userPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(UserPageQueryDTO userPageQueryDTO) {
        PageHelper.startPage(userPageQueryDTO.getPage(),userPageQueryDTO.getPageSize());
        Page<UserRoleVo> page = userMapper.pageQuery(userPageQueryDTO);

        long total = page.getTotal();
        List<UserRoleVo> result = page.getResult();
        log.info("total:{},result:{}",total,result);
        return new PageResult(total,result);
    }

    /**
     * 修改用户信息
     * @param user
     */
    @Override
    public void update(WmsUser user) {
        user.setUpdateTime(LocalDateTime.now());
        user.setUpdateBy(userMapper.getById(BaseContext.getCurrentId()));
        userMapper.update(user);
    }

    /**
     * 新增用户
     * @param user
     */
    @Override
    @Transactional
    public void save(WmsUser user) {
        if (user.getStatus() == null){
            user.setStatus("1");
        }
        user.setUpdateTime(LocalDateTime.now());
        user.setCreateTime(LocalDateTime.now());
        String name = userMapper.getById(BaseContext.getCurrentId());
        user.setUpdateBy(name);
        user.setCreateBy(name);
        userMapper.save(user);
    }

    /**
     * 删除用户
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        userMapper.deleteByIds(ids);
        userRoleMapper.deleteByUserId(ids);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public WmsUser findById(Long id) {
        WmsUser user = userMapper.findById(id);
        return user;
    }

    /**
     * 修改用户角色
     * @param userRoleVo
     */
    @Override
    @Transactional
    public int saveRole(UserRoleVo userRoleVo) {
        log.info("账号状态:{}",userRoleVo.getStatus());
        WmsUser user = new WmsUser();
        BeanUtils.copyProperties(userRoleVo,user);
        save(user);
        if (userRoleVo.getRoleId() == 1L || userRoleVo.getRoleId() == 2L || userRoleVo.getRoleId() == 3L || userRoleVo.getRoleId() == 4L) {
            userRoleMapper.setUserRole(user.getUserId(), userRoleVo.getRoleId());
            return 1;
        }
        return 0;
    }

    /**
     * 修改用户角色
     * @param userRoleVo
     */
    @Override
    @Transactional
    public int updateRole(UserRoleVo userRoleVo) {
        WmsUser user = new WmsUser();
        BeanUtils.copyProperties(userRoleVo,user);
        update(user);
        if (userRoleVo.getRoleId() == 1L || userRoleVo.getRoleId() == 2L || userRoleVo.getRoleId() == 3L || userRoleVo.getRoleId() == 4L) {
            userRoleMapper.update(user.getUserId(), userRoleVo.getRoleId());
            return 1;
        }
        return 0;
    }

    /**
     * 注册
     * @param user
     */
    @Override
    @Transactional
    public void sign(WmsUser user) {
        save(user);
        log.info("用户id:{}",user.getUserId());
        userRoleMapper.setUserRole(user.getUserId(), 4L);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<WmsUser> getAll() {
        return userMapper.getAll();
    }
}
